package com.zdgcc.site.servlet;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.zdgcc.site.domain.Sq;
import com.zdgcc.site.util.CreateImgName;
import com.zdgcc.site.util.Forward;
import com.zdgcc.site.util.Page;
import com.zdgcc.site.util.UploadFile;

public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = 6592535749759139728L;

	private static File tempPathFile;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		this.doPost(request, response);
	}

	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> map = new HashMap<String, String>();
		List<FileItem> imgitems = new ArrayList<FileItem>(); // 图片
		List<String> namelist = new ArrayList<String>(); // 图片名称
		ServletContext servletContext = getServletContext();

		try {
			String uploadPath = servletContext.getRealPath("/") + "files";
			File uploadFile = new File(uploadPath);
			if (!uploadFile.exists()) {
				uploadFile.mkdirs();
			}
			// 为该请求创建一个DiskFileItemFactory对象，通过它来解析请求。
			DiskFileItemFactory factory = new DiskFileItemFactory();
			factory.setSizeThreshold(4096); // 设置缓冲区大小，这里是4kb
			factory.setRepository(tempPathFile);// 设置缓冲区目录
			ServletFileUpload upload = new ServletFileUpload(factory);
			// 设置最大文件尺寸，单位字节 这里是512kb
			upload.setSizeMax(512 * 1024);
			// 执行解析后，所有的表单项目都保存在一个List中。得到所有的文件
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> i = items.iterator();
			while (i.hasNext()) {
				FileItem fi = (FileItem) i.next();
				if (fi.isFormField()) {
					map.put(fi.getFieldName(), new String(fi.getString().getBytes("ISO8859-1"), "UTF-8"));
				} else {
					String fileName = fi.getName();
					if (fileName != null && !"".equals(fileName)) {
						imgitems.add(fi);
						CreateImgName cin = new CreateImgName();
						File fullFile = new File(fi.getName());
						String suffix = fullFile.getName().substring(fullFile.getName().lastIndexOf("."), fullFile.getName().length());
						String imgoo = cin.createImgName(suffix);
						namelist.add(imgoo);
						UploadFile uf = new UploadFile();
						boolean b = uf.uploadFile(uploadPath, 4096, imgitems, namelist);
						System.out.println(b);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		String imgName = namelist.get(0);
		System.out.println("文件名："+imgName);
		request.setAttribute("imgname", imgName);

		List<Sq> list = new ArrayList<Sq>();
		Sq sq = new Sq();
		sq.setId(1);
		sq.setName("name1");
		Sq sq1 = new Sq();
		sq1.setId(2);
		sq1.setName("name2");
		list.add(sq);
		list.add(sq1);
		Page page = new Page(1, 10, 10, list);
		request.setAttribute("page", page);
		Forward.doForward(request, response, "Test.jsp");
	}

}
